<template>
  <div class="app-container">
    <el-row>
      <el-form :inline="true" :model="cartForm" class="demo-form-inline">
        <el-col :span="24"><div class="grid-content bg-purple-dark bg-purple-dark-Top">
          <el-row type="flex" class="row-bg">
            <el-col :span="8"><div class="grid-content bg-purple">
              <el-form-item label="车型编号:">
                <el-input v-model="cartForm.id" placeholder="请输入车型编号" />
              </el-form-item>
            </div></el-col>
            <el-col :span="8"><div class="grid-content bg-purple-light">
              <el-form-item label="应载重量:">
                <el-select v-model="cartForm.allowableLoad" clearable placeholder="请选择应承载重量">
                  <el-option
                    v-for="item in cart_allowableLoad"
                    :key="item.id"
                    :label="item.name"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
            </div></el-col>
            <el-col :span="8"><div class="grid-content bg-purple">
              <el-form-item label="应载体积:">
                <el-select v-model="cartForm.allowableVolume" clearable placeholder="请选择应载体积">
                  <el-option
                    v-for="item in cart_allowableVolume"
                    :key="item.id"
                    :label="item.name"
                    :value="item.value"
                  />

                </el-select>
              </el-form-item>
            </div></el-col>
          </el-row>
          <el-row type="flex" justify="flex-start" class="row-bg row-bg2">
            <el-col :span="8"><div class="grid-content bg-purple">
              <el-form-item label="车辆类型:">
                <el-input v-model="cartForm.name" placeholder="请输入车辆类型" />
              </el-form-item>
            </div></el-col>
            <el-col :span="8"><div class="grid-content bg-purple-light">
              <el-form-item>
                <el-button type="primary" @click="inquire">查询</el-button>
              </el-form-item>
              <el-form-item>
                <el-button native-type="reset" @click="reset">重置</el-button>
              </el-form-item>
            </div></el-col>
          </el-row>
        </div></el-col>

      </el-form>
    </el-row>
    <el-row class="row-bg3">
      <el-col :span="24"><div class="grid-content bg-purple-dark remainder">
        <el-row>
          <div>
            <el-button type="primary" style="margin-bottom: 20px;" @click="addType">新增车型</el-button>
          </div>
        </el-row>
        <el-table
          v-loading="loading"
          :data="tableData"
          stripe
          style="width: 100%"
        >
          <el-table-column
            type="index"
            label="序号"
            width="60"
          />
          <el-table-column
            prop="id"
            label="车型编号"
            width="180"
          />
          <el-table-column
            prop="name"
            label="车辆类型"
          />
          <el-table-column
            prop="num"
            label="车辆数量"
          />
          <el-table-column
            prop="allowableLoad"
            label="应载重量(吨)"
          />
          <el-table-column
            prop="allowableVolume"
            label="应载体积(立方米)"
          />
          <el-table-column
            prop="measureLong"
            label="长(米)"
          />
          <el-table-column
            prop="measureWidth"
            label="宽(米)"
          />
          <el-table-column
            prop="measureHigh"
            label="高(米)"
          />
          <el-table-column
            fixed="right"
            label="操作"
            width="100"
            align="center"
          >
            <template slot-scope="scope">
              <el-button type="text" size="small" class="View_BUtton" :visible.sync="dialogFormVisible" @click="edit(scope.row.id)">编辑</el-button>
              <div data-v-fadabbee="" class="el-divider el-divider--vertical" />

              <el-button type="text" size="small" @click="del(scope.row.id)">删除</el-button>

            </template>
          </el-table-column>

        </el-table>
        <div class="block" style="text-align: center; margin-top: 20px;">

          <el-pagination
            :current-page="currentPage4"
            :page-sizes="[10, 20, 30, 50]"
            :page-size="pagination.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pagination.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div></el-col>
    </el-row>
    <el-dialog width="580px" :title="showTitle" :visible.sync="dialogFormVisible" class="dialog">

      <el-form ref="cartAddForm" :model="cartAddForm" :rules="cartRules">
        <el-form-item prop="name" label="车辆类型" :label-width="formLabelWidth">
          <el-input v-model="cartAddForm.name" class="dialog_input" autocomplete="off" placeholder="请输入车辆类型" />
        </el-form-item>
        <el-form-item prop="allowableLoad" label="应载重量" :label-width="formLabelWidth">
          <el-input v-model="cartAddForm.allowableLoad" class="dialog_input" autocomplete="off" placeholder="请输入车辆重量" />
        </el-form-item>
        <el-form-item prop="allowableVolume" label="应载体积" :label-width="formLabelWidth">
          <el-input v-model="cartAddForm.allowableVolume" class="dialog_input" autocomplete="off" placeholder="请输入车辆体积" />
        </el-form-item>
        <el-form-item prop="measureLong" label="长" :label-width="formLabelWidth">
          <el-input v-model="cartAddForm.measureLong" class="dialog_input" autocomplete="off" />
        </el-form-item>
        <el-form-item prop="measureWidth" label="宽" :label-width="formLabelWidth">
          <el-input v-model="cartAddForm.measureWidth" class="dialog_input" autocomplete="off" />
        </el-form-item>
        <el-form-item prop="measureHigh" label="高" :label-width="formLabelWidth">
          <el-input v-model="cartAddForm.measureHigh" class="dialog_input" autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button style="text-align: center;" @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="fnOK">确 定</el-button>
      </div>
    </el-dialog>

  </div>

</template>

<script>
import { renderType, addType, editType, updateType, delType } from '@/api/cart'
export default {
  data() {
    return {
      // 车型载重对应关系:
      cart_allowableLoad: [{
        value: 'RANGE_LEVEL_1',
        name: '<1.8（吨）'
      },
      {
        value: 'RANGE_LEVEL_2',
        name: '1.8-6（吨）'
      },
      {
        value: 'RANGE_LEVEL_3',
        name: '6-14（吨）'
      },
      {
        value: 'RANGE_LEVEL_4',
        name: '14-30（吨）'
      },
      {
        value: 'RANGE_LEVEL_5',
        name: '30-50（吨）'
      },
      {
        value: 'RANGE_LEVEL_6',
        name: '50-100（吨）'
      },
      {
        value: 'RANGE_LEVEL_7',
        name: '100>（吨）'
      }],
      // 车型体积对应关系:
      cart_allowableVolume: [{
        value: 'RANGE_LEVEL_1',
        name: '<3（m³）'
      },
      {
        value: 'RANGE_LEVEL_2',
        name: '3-5（m³）'
      },
      {
        value: 'RANGE_LEVEL_3',
        name: '5-10（m³）'
      },
      {
        value: 'RANGE_LEVEL_4',
        name: '10-15（m³）'
      },
      {
        value: 'RANGE_LEVEL_5',
        name: '15-30（m³）'
      },
      {
        value: 'RANGE_LEVEL_6',
        name: '30-50（m³）'
      },
      {
        value: 'RANGE_LEVEL_7',
        name: '50-80（m³）'
      },
      {
        value: 'RANGE_LEVEL_8',
        name: '80-150（m³）'
      },
      {
        value: 'RANGE_LEVEL_9',
        name: '150>（m³）'
      }],
      loading: true, // 加载状态
      editTypeId: '', // 点击编辑获取到的id
      // 搜索框数据
      cartForm: {
        id: '', // 车型编号
        allowableLoad: '', // 载重
        allowableVolume: '', // 体积
        name: '', // 车型类型
        measureLong: 0.0, // 长
        measureWidth: 0.0, // 宽
        measureHigh: 0.0, // 高
        page: 1, // 页码
        pageSize: 10 // 每页多少条
      },
      // 新增数据
      cartAddForm: {
        allowableLoad: null, // 载重
        allowableVolume: null, // 体积
        name: '', // 车型类型
        measureLong: 0, // 长
        measureWidth: 0, // 宽
        measureHigh: 0, // 高
        goodsTypes: []
      },
      // 校验规则
      cartRules: {
        name: [
          { required: true, message: '车型不能为空', trigger: 'blur' }
        ],
        allowableLoad: [
          { required: true, message: '只能输入1-100之间的正整数，请重新输入', trigger: 'blur' }
          // { type: 'number', message: '重量必须为数字值' }
        ], // 载重
        allowableVolume: [
          { required: true, message: '只能输入1-100之间的正整数，请重新输入', trigger: 'blur' }
          // { type: 'number', message: '体积必须为数字值' }
        ], // 体积
        measureLong: [
          { required: false, message: '长最多只能输入100，请重新输入', trigger: 'blur' }
          // { type: 'number', message: '长必须为数字值' }
        ], // 长
        measureWidth: [
          { required: false, message: '宽最多只能输入100，请重新输', trigger: 'blur' }
          // { type: 'number', message: '宽必须为数字值' }
        ], // 宽
        measureHigh: [
          { required: false, message: '高最多只能输入100，请重新输', trigger: 'blur' }
          // { type: 'number', message: '高必须为数字值' }
        ] // 高
      },
      // 分页数据  控制页码变量
      pagination: {
        // page: 1, // 页码
        // pageSize: 10, // 每页多少条
        total: null // 总条数
      },
      currentPage4: 1, // 前往 第几页默认数据
      tableData: [], // 分页表格数据
      // 对话框 数据
      dialogTableVisible: false,
      dialogFormVisible: false,
      formLabelWidth: '120px'
    }
  },
  computed: {
    showTitle() {
      if (this.cartAddForm.id) {
        return '编辑车型'
      } else {
        return '新增车型'
      }
    }
  },

  created() {
    this.renderType()
  },
  methods: {
    // 基础渲染 分页数据
    async renderType() {
      this.loading = true
      const result = await renderType(
        this.cartForm
      )
      console.log(result)
      this.tableData = result.items
      this.pagination.total = +result.counts
      console.log(this.pagination.total)
      this.loading = false
    },
    // 分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.cartForm.pageSize = val
      this.renderType()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.cartForm.page = val
      console.log(this.cartForm.page)
      this.renderType()
    },
    // 点击新增按钮
    addType() {
      this.dialogFormVisible = true
      // 初始化数据
      this.cartAddForm = {
        allowableLoad: null, // 载重
        allowableVolume: null, // 体积
        name: '', // 车型类型
        measureLong: 0, // 长
        measureWidth: 0, // 宽
        measureHigh: 0// 高
      }
    },
    // 新增/编辑车型 点击确定
    async fnOK() {
      if (this.showTitle === '新增车型') {
        this.dialogFormVisible = false
        const result = await addType(
          this.cartAddForm
        )
        console.log(result)
        console.log('123')
        this.$message.success('新增车型成功')
        // this.$refs.cartAddForm.resetFields()
        // 重新渲染
        this.renderType()
      } if (this.showTitle === '编辑车型') {
        this.dialogFormVisible = false
        // const result = await editType(this.cartAddForm.id)
        // console.log(result)
        console.log(this.cartAddForm)
        await updateType(this.cartAddForm, this.editTypeId)
        this.$message.success('车型信息修改成功')
        // 重新渲染
        this.renderType()
      }
    },
    // 点击编辑
    async edit(id) {
      // this.cartAddForm.id = id
      this.editTypeId = id
      this.dialogFormVisible = true
      // console.log(id)
      const result = await editType(id)
      this.cartAddForm = result
      this.cartAddForm.id = Number(result.id)
      this.cartAddForm.goodsTypes = []
      console.log(this.cartAddForm.id)
      console.log(result)
    },
    // 删除对话框
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    },
    // 点击 删除
    async del(id) {
      await this.$confirm('是否要删除此车型?', '删除确认')
      await delType(id)
      this.$message.success('删除成功')
      this.renderType()
    },
    // 点击查询
    inquire() {
      this.renderType()
      console.log(this.cartForm)
    },
    // 点击重置 先将数据初始化在发请求
    reset() {
      this.cartForm.id = '' // 车型编号
      this.cartForm.allowableLoad = '' // 载重
      this.cartForm.allowableVolume = '' // 体积
      this.cartForm.name = '' // 车型类型
      this.renderType()
    }
  }
}

</script>

<style lang="scss" scoped>
  .el-row {
/*     margin-bottom: 20px; */
    &:last-child {
      margin-bottom: 0;
    }
  }
  .el-col {
    border-radius: 4px;
  }
  .bg-purple-dark-Top {
    background: #fff;
    padding:10px 8px 0px 50px
  }

  .grid-content {
    border-radius: 4px;
    min-height: 36px;

  }
  .row-bg {
    padding: 10px 0;
/*     background-color: #f9fafc; */
  }
// .el-input,
// .el-select
// {
// width: 400px;
// }
.bg-purple{
  display: flex;
}

.View_BUtton{
  color: #419eff;
}
.row-bg2{
  margin-top: -20px;
}
.row-bg3{
  margin-top: 20px;
  background-color: #fff;
  padding: 20px;
}
::v-deep .el-dialog__header{
  border-radius: 10px;
}
::v-deep .el-dialog{
  border-radius: 10px;
  background-color: #f3f4f7;
}
::v-deep .el-dialog__body{
  background-color: #fff;
  // text-align: center;
}
::v-deep .el-dialog__footer{
  background-color: #fff;
  border-radius: 10px;
  text-align: center;
}
// .el-table{
//     border: 1px solid #ebeef5;
//   }
.dialog_input{
  width: 350px;
}
</style>

